package com.cyzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyzy.mapper.RolePermissionMapper;
import com.cyzy.pojo.AdminUser;
import com.cyzy.pojo.RolePermission;
import com.cyzy.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper,RolePermission> implements RolePermissionService {
    @Autowired
    private RolePermissionMapper permissionMapper;

    @Override
    public List<RolePermission> selectRolePermissionList() {
        return permissionMapper.selectRolePermissionList();
    }

    @Override
    public List<RolePermission> searchworkersAll(String menuName,String role, Integer rolePermissionState) {
        return permissionMapper.searchworkersAll(menuName,role,rolePermissionState);
    }

    @Override
    public List<RolePermission> selectRolePermissionById(String role) {
        return permissionMapper.selectRolePermissionById(role);
    }

    @Override
    public int updateManage(Integer roleId, String role, Integer rolePermissionState, String description) {
        UpdateWrapper<RolePermission> wrapper = new UpdateWrapper<>();
        wrapper.eq("role_id", roleId)
                .set("role", role)
                .set("role_permission_state", rolePermissionState)
                .set("description", description);

        return permissionMapper.update(null, wrapper);
    }


    @Override
    public int deleteState(String id, String state) {
        UpdateWrapper<RolePermission> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("role_id", id); // 条件：匹配 name

        RolePermission rolePermission = new RolePermission();
        rolePermission.setRolePermissionState(2); // 状态改为 2

        return permissionMapper.update(rolePermission, updateWrapper);
    }

    @Override
    public int deleteStateByIds(List<String> ids, String state) {
        UpdateWrapper<RolePermission> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("role_id", ids); // 条件：ID 在传入的列表中

        RolePermission rolePermission = new RolePermission();
        rolePermission.setRolePermissionState(2); // 状态设为 2 表示删除（逻辑删除）

        return permissionMapper.update(rolePermission, updateWrapper); // 返回受影响行数
    }

    @Override
    public List<RolePermission> selectRolePermissionList1() {
        return permissionMapper.selectRolePermissionList1();
    }
}
